﻿using Xunit;
using YAPL.Language.Ast;

namespace YAPL.Test.YAPL.Language.Ast {

	public class AssignmentExpressionTest {
		[Fact]
		public void NodeEqualsShouldWorkCorrectly() {
			var a1 = Expressions.Assignment(Expressions.Variable("a"), Expressions.Variable("b"));
			var a2 = Expressions.Assignment(Expressions.Variable("a"), Expressions.StringLiteral("literal"));

			Assert.False(a1.NodeEquals(null));
			Assert.False(a1.NodeEquals(Expressions.Variable("b")));
			Assert.True(a1.NodeEquals(a2));
		}

		[Fact]
		public void RewriteChildSHouldWorkCorrectly() {
			var a = Expressions.Variable("a");
			var b = Expressions.Variable("b");
			var assignment = Expressions.Assignment(a, b);
			var r1 = assignment.RewriteChild(b, Expressions.Integer(1));
			var r2 = assignment.RewriteChild(a, Expressions.Variable("c"));

			Assert.True(r1);
			Assert.True(r2);

			Assert.IsType(typeof(ConstantExpression), assignment.Right);
			var i1 = (ConstantExpression)assignment.Right;
			Assert.Equal(1, (int)i1.Value);

			Assert.IsType(typeof(VariableExpression), assignment.Left);
			var c = (VariableExpression)assignment.Left;
			Assert.Equal("c", c.Name);
		}
	}
}
